﻿using UnityEngine;
using System.Collections;

public class RollBossTarget : MonoBehaviour {
	#region Member
	public VoidDelegate OnDeathAniPlay;
	[SerializeField]UILabel labHPCount;
	[SerializeField]UISlider scrollBar;
	[SerializeField]UISprite bgSprite;
	[SerializeField]UISprite foreWidght;
	[SerializeField]float speed = 1;
	[SerializeField]string[] colors;
	int colorslength{get{ return colors.Length - 1; }}

	float _targetvalue;
	float _nowvalue;
	float _v;
	bool _playing;
	public float mindistance = 0.01f;
	#endregion
	void Awake () {
		_nowvalue = colorslength;
		_targetvalue = _nowvalue;

	}

	// Update is called once per frame
	void Update () {
		if (_playing) {
			float v = 0;
			v = Mathf.SmoothDamp (_nowvalue, _targetvalue, ref _v, speed);
			if (Mathf.Abs (_v) <= mindistance) {
				_playing = false; 
				UpdateValue (_targetvalue);
				if (_targetvalue == 0) {
					if (OnDeathAniPlay != null)
						OnDeathAniPlay ();
				}
			} else {
				UpdateValue (v);
			}
		}
	}
	void UpdateValue(float value){
		int index = (int)_nowvalue;
		_nowvalue = value;
		scrollBar.value = _nowvalue%1;
		if (index != (int)_nowvalue) {
			//滚动条数改变了
			foreWidght.spriteName = colors[(int)_nowvalue+1];
			bgSprite.spriteName = colors[(int)_nowvalue];
			SetLabelCount ((int)_nowvalue+1);
		}
	}
	public void SetValue(float value){
		Mathf.Clamp (value, 0, 1f);
		_playing = true;
		_targetvalue = value * colorslength;
	}
	public void SetValueNow(float value){
		Mathf.Clamp (value, 0, 1f);
		_playing = false;
		UpdateValue (value * colorslength);

	}
	public void SetInitBossValue(float value){
		_nowvalue = 0f;
		_targetvalue = value * colorslength-0.0001f;
		_playing = true;

	}

	public void SetLabelCount(int value){
		labHPCount.text = "X" +""+ value.ToString ();
	}

	public void HideBossHP(){
		_playing = true;
		_targetvalue = 0;
	}
}
